package components.plugins.search
{
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_checkbox;
	import UIFree.uif_component;
	import UIFree.uif_loader;
	
	import UIUtils.uif_cache;
	
	import classes.clt_events;
	import classes.clt_locale;
	
	import components.clt_component;
	import components.google.clt_searchLayer;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class clt_searchLayerTool extends clt_component {
		private var _searchLayer: clt_searchLayer;
		private var _checkBox: uif_checkbox;
		public function clt_searchLayerTool(a_clt: clt_clothingBase) {
			var rect: Rectangle = a_clt.tools.toolLayer.getInner();
			super(a_clt.tools.toolLayer, rect.x, rect.y, rect.width - 30, rect.height);
		}
		
		override protected function createComponents():void {
			super.createComponents();
			var rect: Rectangle = getInner();
			_searchLayer = new clt_searchLayer(this, new uif_cache, rect.x, rect.y, rect.width, rect.height - 30);
			_searchLayer.addEvent(Event.SELECT, doSelectImage);
			
			_checkBox = new uif_checkbox(this, rect.x + 130, rect.y + rect.height - 20, 100, 20, 'checkbox');
			_checkBox.group = 'lt';
			_checkBox.text = clt_locale.ASPHOTO;
			
			_checkBox = new uif_checkbox(this, rect.x, rect.y + rect.height - 20, 130, 20, 'checkbox');
			_checkBox.group = 'lt';
			_checkBox.text = clt_locale.ASBACKGROUND;
			_checkBox.checked = true;
		}
		
		private function doSelectImage(e: ObjectEvent): void {
			var imageURL: String = clt_servers.GETIMAGEURL + e.object.link;
			
//			workspace.loadPhoto(imageURL);
			if (_checkBox.checked) {
				function doComplete(e: Event): void {
					loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, doComplete);
					workspace.dispatchEvent(new ObjectEvent(clt_events.LOADBACKGROUND, loader));
				}
				var loader: uif_loader = new uif_loader(null, imageURL, doComplete);
			} else workspace.loadPhoto(imageURL);
/*			
			var loader: uif_loader
			clt.workspace.dispatchEvent(new ObjectEvent(clt_events.LOADBACKGROUND, ));*/
		}
		
		public function execute(a_params: Object): void {
			clt.workspace.dispatchEvent(new ObjectEvent(clt_events.SHOWTOOLS, this));
		}
	}
}